package models.shop;

import play.db.jpa.GenericModel;

import javax.persistence.*;
import java.util.List;

@Entity()
@Table(name = "products")
public class Product extends GenericModel {
    @Column(name = "createdByUser")
    public Integer createdByUserId;

    public Product() {

    }

    public Product(int id, String title, double price, String shortDescription) {
        this.id = id;
        this.title = title;
        this.price = price;
        this.shortDescription = shortDescription;
    }

    public Product(int id, String title, double price, String shortDescription, String description, int imagesCount) {
        this(id, title, price, shortDescription, imagesCount);
        this.description = description;
    }

    public Product(int id, String title, double price, String shortDescription, String description, int imagesCount, boolean archived) {
        this(id, title, price, shortDescription, description, imagesCount);
        this.archived = archived;
    }

    public Product(int id, String title, double price, String shortDescription, int imagesCount) {
        this(id, title, price, shortDescription);
        this.imagesCount = imagesCount;
    }

    public Product(int id, String title, double price, String shortDescription, String description, int imagesCount, Category category) {
        this(id, title, price, shortDescription, description, imagesCount);
        this.category = category;
    }

    public Product(int id, String title, double price, String shortDescription, String description, int imagesCount, Category category, boolean archived) {
        this(id, title, price, shortDescription, description, imagesCount, category);
        this.archived = archived;
    }

    @Transient
    public boolean getHasPicture() {
        return imagesCount > 0;
    }

    @Transient
    public long imagesCount = 0;

    @Id
    @Column(name = "productId")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int id;
    public String title;
    public double price;
    public String description;
    public String shortDescription;
    @Transient
    public String brandName;
    @Transient
    public int categoryId;
    @OneToOne(optional = true, fetch = FetchType.LAZY)
    @JoinTable(name = "products_categories",
            joinColumns = {
                    @JoinColumn(name = "productId", unique = true)
            },
            inverseJoinColumns = {
                    @JoinColumn(name = "categoryId")
            }
    )
    public Category category;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "brandId")
    public Brand brand;
    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "product_images",
            joinColumns = {
                    @JoinColumn(name = "productId")
            },
            inverseJoinColumns = {
                    @JoinColumn(name = "imageId")
            }, uniqueConstraints = {@UniqueConstraint(columnNames = {"productId", "imageId"})}
    )
    public List<Image> images;
    //TODO: It's created for compatibility. Must be removed later
    @Transient
    public String productSources;
    @OneToMany
    @JoinTable(name = "product_content_source_urls",
            joinColumns = {
                    @JoinColumn(name = "productId")
            },
            inverseJoinColumns = {
                    @JoinColumn(name = "contentSourceUrlId")
            }, uniqueConstraints = {@UniqueConstraint(columnNames = {"contentSourceUrlId", "productId"})}
    )
    public List<ContentSourceUrl> contentSourceUrls;
    @Column(name = "visitCounter")
    public int visitsCount;
    public boolean archived;
}
